//
//  UIView.swift
//  
//
//  Created by tangbl93 on 2021/1/6.
//

import UIKit

public extension UIView {
    
    /// RTL Mode Trigger
    static var yx_enableRTLMode: Bool = false
    
    /// isVisible
    var yx_isVisible: Bool {
        get { !isHidden }
        set { isHidden = !newValue }
    }
    
    /// current is RTL Layout
    var yx_isRTL: Bool {
        if Self.yx_enableRTLMode {
            return UIView.appearance().semanticContentAttribute == .forceRightToLeft
        }
        return false
    }
    
    /// snapshot after screenUpdates
    var yx_snapshot:UIImage? {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.main.scale)
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        self.layer.render(in: context)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    
    /// snapshot without screenUpdates
    var yx_fastSnapshot:UIImage? {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.main.scale)
        self.drawHierarchy(in: bounds, afterScreenUpdates: false)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    
    /// snapshot & resize & writeToSavedPhotosAlbum
    func yx_writeToSavedPhotosAlbum(after screenUpdates:Bool = false, size: CGSize? = nil, completeHandler: UIImage.YXImageWriteToSavedPhotosAlbumHandler? = nil) {
        let snapshot = screenUpdates ? self.yx_snapshot : self.yx_fastSnapshot
        if let size = size {
            snapshot?.yx_resize(to: size)?.yx_writeToSavedPhotosAlbum(completeHandler: completeHandler)
        } else {
            snapshot?.yx_writeToSavedPhotosAlbum(completeHandler: completeHandler)
        }
    }
}

// MARK: CALayer Ext
public extension UIView {
    
    /// rounded view
    var yx_rounded: Bool {
        get {
            let rounded = self.layer.cornerRadius == (yx_height / 2.0)
            let masksToBounds = self.layer.masksToBounds
            return rounded && masksToBounds
        }
        set {
            if newValue {
                self.layer.cornerRadius = yx_height / 2.0
                self.layer.masksToBounds = true
            } else {
                self.layer.cornerRadius = 0
                self.layer.masksToBounds = false
            }
        }
    }
    
    /// layer.cornerRadius
    var yx_cornerRadius: CGFloat {
        get {
            return self.layer.cornerRadius
        }
        set(cornerRadius) {
            self.layer.cornerRadius = cornerRadius
            self.layer.masksToBounds = true
        }
    }
    
    /// layer.borderColor
    var yx_borderColor: UIColor? {
        get {
            guard let borderColor = self.layer.borderColor else { return nil }
            return UIColor(cgColor: borderColor)
        }
        set(borderColor) {
            self.layer.borderColor = borderColor?.cgColor
        }
    }
    
    /// layer.borderWidth
    var yx_borderWidth: CGFloat {
        get { return self.layer.borderWidth }
        set { self.layer.borderWidth = newValue }
    }
    
    /// cornerRadius with corners
    func yx_cornerRadius(radius: CGFloat, corners: UIRectCorner, contentSize: CGSize? = nil) {
        self.layer.masksToBounds = true
        
        if #available(iOS 11.0, *) {
            self.layer.cornerRadius = radius
            self.layer.maskedCorners = CACornerMask(rawValue: corners.rawValue)
        } else {
            let cornerRadii = CGSize(width: radius, height: radius)
            let path: UIBezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: cornerRadii)
            
            let maskLayer: CAShapeLayer = CAShapeLayer()
            if let contentSize = contentSize {
                maskLayer.frame = CGRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height)
            } else {
                maskLayer.frame = bounds
            }
            maskLayer.path = path.cgPath
            
            self.layer.mask = maskLayer
        }
    }
}
